<template>
	<view class="contain">
		<!-- heightFix -->
		<view class="head w-[100%]"><image src="https://guangxins.oss-cn-shenzhen.aliyuncs.com/images/bg/buybg.jpg" mode="widthFix" style="width:100%;"></image></view>
		<view class="m-auto w-[80%] flex justify-between mb-5">
			<view class="blod" @click="goOrders">←购买记录</view>
			<view class="blod" @click="gomine">个人中心→</view>
		</view>
		
		<view class="taocan">
			<view class="box">
				<image @click="buymode('测评屋')" src="https://guangxins.oss-cn-shenzhen.aliyuncs.com/images/bg/buyxinli.png" mode="aspectFill"></image>
				</view>
		</view> <!-- taocan -->
	</view> <!-- contain -->
</template>

<script setup lang='ts'>
import { ref,reactive } from 'vue'
import {  onLoad,onShow } from '@dcloudio/uni-app'
import {generateOrder,payOrder} from '@/utils/utils'
import { userStore } from '@/stores/userStore'
let { hasLogin,userData } = userStore();


let isCanBuy=ref(true); //是否已购买过当前课程

let realPayMoney=ref(1);//7600
let merchant=reactive<any>({
	buyerUserId:'', //购买者id
	name:'心理筛查',  //分类统一使用 "心理筛查"
	tel:'',
	adr:'',
	class:'健康',
	desc:'量表测评',
	photo:'',
	month:'1',
	price:realPayMoney.value
	, //测试金额 0.01 127100
	lat:20.12345,
	lng:110.666588,
})

const checkLogin=()=>{
	let msg="请登录后操作";
	if(hasLogin===false || userData.userinfo===null){
			msg="请登录后购买，点击【确定】登录";
			uni.showModal({
					title:"温馨提示",
					content:msg,
					showCancel:true,
					success:res=>{
						if(res.confirm){
							uni.$tools.GoUrl("/pages/login/login",1);
						}
					}
			})
			return false;
	}
	return true;
}



const goOrders=()=>{
	uni.$tools.GoUrl("/pages/sjorders/sjorders");
}

const gomine=()=>{
	uni.$tools.GoUrl("/pages/mine/mine",2);
}

const buymode=(mode:string)=>{
	
	if(!checkLogin()){
		return;
	}//检查登录
	
	uni.$tools.Toast(mode);
	merchant.class=mode;
	GenerateOrder();
}
let loginCode=ref("");

onLoad(()=>{
	// let paraPayData:any={};
	// uni.$tools.Reqeust("/api/aaa",paraPayData,'POST').then((res:any)=>{
	// 	console.log("/api/aaa:",res);
	// })	
	
	uni.login({
		  provider: 'weixin',
		  onlyAuthorize:true,
		  success:e=> {
			//登录。注册会员信息
			if(e.errMsg==='login:ok' && e.code!=null){
				loginCode.value=e.code;
				console.log("微信 login code:",loginCode.value);							
			}
		  }
	});
});

onShow(()=>{
	GetCePingPrice();
	if(userData.userinfo.id!=null){
		merchant.buyerUserId=userData.userinfo.id;
		console.log("当前用户id",merchant.buyerUserId);
	}
});


//获取支付价格
const GetCePingPrice=()=>{
	let paraData={};
	uni.$tools.Reqeust('/api/getCePingPrice',paraData,'POST').then((res:any)=>{
		console.log("获取获取支付价格",res);
		if(res.data.code===200){
			if(res.data.definedprice!=null){
				realPayMoney.value=res.data.definedprice.setmoney;
				merchant.price=res.data.definedprice.setmoney;
				console.log("支付金额",realPayMoney.value);
			}
		}else{
			uni.$tools.Toast(res.data.msg);
		}
	});
}

//生成订单
const GenerateOrder=async ()=>{
	// if(!/\S/.test(merchant.name)){
	// 	uni.$tools.Toast("给店铺起个名称")
	// 	return;
	// }
	// if(!uni.$u.test.chinese(merchant.name)){
	// 	uni.$tools.Toast("商家名称请输入全中文")
	// 	return;
	// }
	// if(!/\S/.test(merchant.tel)){
	// 	uni.$tools.Toast("填写联系方式")
	// 	return;
	// }
	// if(!uni.$u.test.mobile(merchant.tel)){
	// 	uni.$tools.Toast("手机号码格式不正确")
	// 	return;
	// }
	// if(merchant.adr==='请选择' || !/\S/.test(merchant.adr)){
	// 	uni.$tools.Toast("请选择商家地址")
	// 	return;
	// }
	// if(merchant.class==='请选择' || !/\S/.test(merchant.class)){
	// 	uni.$tools.Toast("请选择所属分类")
	// 	return;
	// }
	// if(merchant.desc==='' || !/\S/.test(merchant.desc)){
	// 	uni.$tools.Toast("请填写商家介绍")
	// 	return;
	// }
	// if(merchant.photo==='' || !/\S/.test(merchant.photo)){
	// 	uni.$tools.Toast("请上传一张商家店铺或店招环境图片")
	// 	return;
	// }
	// if(merchant.month==='' || !/\S/.test(merchant.month)){
	// 	uni.$tools.Toast("请选择入驻时长")
	// 	return;
	// }
	// if(merchant.price<=0){
	// 	uni.$tools.Toast("请选择入驻时长")
	// 	return;
	// }
	if(merchant.buyerUserId===null || merchant.buyerUserId===''){
		uni.$tools.Toast("当前购买者id已失效")
		setTimeout(()=>{
			uni.$tools.GoUrl("/pages/login/login",0); //跳转到登录
		},1000);
		return;
	}
	
	uni.showLoading({
		title:"正在支付"
	})
	setTimeout(()=>{
		uni.hideLoading();
	},8000);
	/*生成订单*/
	let paraData={
		userid:merchant.buyerUserId,
		ordername:merchant.name,
		adrphone:merchant.tel,
		adradress:merchant.adr,
		productname:merchant.class, //productname 购买套餐分类
		adrdetail:merchant.desc, //商家简介
		totalpay:merchant.price, //实际支付金额
		productimg:merchant.photo, //商家图片
		buynumber:merchant.month.replace("个月",""), //入驻时长
		//beizhu:merchant.lat+","+merchant.lng, //经纬度 lat,lng 逗号分隔 (20.12345,110.666588)，后台分解
		beizhu:'',
		orderprice:merchant.price //支付金额
	}
	console.log("下单参数:",paraData);
	generateOrder(paraData).then(res=>{
		console.log("服务器返回",res);
		if(res.data.code===200){
			let orderNo=res.data.orderInfo.orderno;
			let payMoney=merchant.price;
			let productName=merchant.class;
			//生成订单后，上传商家订单号+图片店招 更新
			//uploadMerchantImage(orderNo);
			//调用支付订单
			payOrder(orderNo,productName,realPayMoney.value);
		}		
	});
}


</script>

<style>
	page{
		background:#f6eade;
	}
</style>
<style lang='scss' scoped>
	@import "cepingbuy.scss";
</style>